using Coding4Fun.Phone.Controls.Toolkit;
using Coding4Fun.Phone.Controls.Toolkit.Primitives;
using Coding4Fun.Phone.Controls.Toolkit.Properties;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
namespace Coding4Fun.Phone.Controls.Primitives
{
	public abstract class ValuePickerBasePage<T> : PhoneApplicationPage, IValuePickerPage<T> where T : struct
	{
		private const string VisibilityGroupName = "VisibilityStates";
		private const string OpenVisibilityStateName = "Open";
		private const string ClosedVisibilityStateName = "Closed";
		private static readonly string StateKeyValue = "ValuePickerPageBase_State_Value" + typeof(T);
		private LoopingSelector _primarySelectorPart;
		private LoopingSelector _secondarySelectorPart;
		private LoopingSelector _tertiarySelectorPart;
		private Storyboard _closedStoryboard;
		private T? _value;
		public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register("DialogTitle", typeof(string), typeof(ValuePickerBasePage<T>), new PropertyMetadata(""));
		public virtual T? Value
		{
			get
			{
				return this._value;
			}
			set
			{
				this._value = value;
				this.method_6();
			}
		}
		public string DialogTitle
		{
			get
			{
				return (string)base.GetValue(ValuePickerBasePage<T>.DialogTitleProperty);
			}
			set
			{
				base.SetValue(ValuePickerBasePage<T>.DialogTitleProperty, value);
			}
		}
		protected ValuePickerBasePage()
		{
			base.set_DataContext(this);
		}
		protected void InitializeValuePickerPage(LoopingSelector primarySelector, LoopingSelector secondarySelector, LoopingSelector tertiarySelector)
		{
			if (primarySelector == null)
			{
				throw new ArgumentNullException("primarySelector");
			}
			if (secondarySelector == null)
			{
				throw new ArgumentNullException("secondarySelector");
			}
			if (tertiarySelector == null)
			{
				throw new ArgumentNullException("tertiarySelector");
			}
			this._primarySelectorPart = primarySelector;
			this._secondarySelectorPart = secondarySelector;
			this._tertiarySelectorPart = tertiarySelector;
			this._primarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.method_0);
			this._secondarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.method_0);
			this._tertiarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.method_0);
			this._primarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.method_1);
			this._secondarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.method_1);
			this._tertiarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.method_1);
			this._primarySelectorPart.set_Visibility(1);
			this._secondarySelectorPart.set_Visibility(1);
			this._tertiarySelectorPart.set_Visibility(1);
			int num = 0;
			using (IEnumerator<LoopingSelector> enumerator = this.GetSelectorsOrderedByCulturePattern().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					LoopingSelector current = enumerator.get_Current();
					Grid.SetColumn(current, num);
					current.set_Visibility(0);
					num++;
				}
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				IEnumerator enumerator2 = VisualStateManager.GetVisualStateGroups(frameworkElement).GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						VisualStateGroup visualStateGroup = (VisualStateGroup)enumerator2.get_Current();
						if ("VisibilityStates" == visualStateGroup.get_Name())
						{
							IEnumerator enumerator3 = visualStateGroup.get_States().GetEnumerator();
							try
							{
								while (enumerator3.MoveNext())
								{
									VisualState visualState = (VisualState)enumerator3.get_Current();
									if ("Closed" == visualState.get_Name() && visualState.get_Storyboard() != null)
									{
										this._closedStoryboard = visualState.get_Storyboard();
										this._closedStoryboard.add_Completed(new EventHandler(this.method_5));
									}
								}
							}
							finally
							{
								IDisposable disposable = enumerator3 as IDisposable;
								if (disposable != null)
								{
									disposable.Dispose();
								}
							}
						}
					}
				}
				finally
				{
					IDisposable disposable2 = enumerator2 as IDisposable;
					if (disposable2 != null)
					{
						disposable2.Dispose();
					}
				}
			}
			if (base.get_ApplicationBar() != null)
			{
				IEnumerator enumerator4 = base.get_ApplicationBar().get_Buttons().GetEnumerator();
				try
				{
					while (enumerator4.MoveNext())
					{
						object current2 = enumerator4.get_Current();
						IApplicationBarIconButton applicationBarIconButton = current2 as IApplicationBarIconButton;
						string text;
						if (applicationBarIconButton != null && (text = applicationBarIconButton.get_Text()) != null)
						{
							if (!(text == "DONE"))
							{
								if (text == "CANCEL")
								{
									applicationBarIconButton.set_Text(Resources.ValuePickerCancelText);
									applicationBarIconButton.add_Click(new EventHandler(this.method_3));
								}
							}
							else
							{
								applicationBarIconButton.set_Text(Resources.ValuePickerDoneText);
								applicationBarIconButton.add_Click(new EventHandler(this.method_2));
							}
						}
					}
				}
				finally
				{
					IDisposable disposable3 = enumerator4 as IDisposable;
					if (disposable3 != null)
					{
						disposable3.Dispose();
					}
				}
			}
			VisualStateManager.GoToState(this, "Open", true);
		}
		private void method_0(object sender, SelectionChangedEventArgs e)
		{
			DataSource<T> dataSource = (DataSource<T>)sender;
			this._primarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
			this._secondarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
			this._tertiarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
		}
		private void method_1(object sender, DependencyPropertyChangedEventArgs e)
		{
			if ((bool)e.get_NewValue())
			{
				this._primarySelectorPart.IsExpanded = (sender == this._primarySelectorPart);
				this._secondarySelectorPart.IsExpanded = (sender == this._secondarySelectorPart);
				this._tertiarySelectorPart.IsExpanded = (sender == this._tertiarySelectorPart);
			}
		}
		private void method_2(object sender, EventArgs e)
		{
			this._value = new T?(((ValueWrapper<T>)this._primarySelectorPart.DataSource.SelectedItem).Value);
			this.method_4();
		}
		private void method_3(object sender, EventArgs e)
		{
			this._value = default(T?);
			this.method_4();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (cancelEventArgs_0 == null)
			{
				throw new ArgumentNullException("e");
			}
			cancelEventArgs_0.set_Cancel(true);
			this.method_4();
		}
		private void method_4()
		{
			if (this._closedStoryboard != null)
			{
				VisualStateManager.GoToState(this, "Closed", true);
				return;
			}
			this.method_5(null, null);
		}
		private void method_5(object sender, EventArgs e)
		{
			base.get_NavigationService().GoBack();
		}
		protected abstract IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern();
		protected static IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern(string pattern, char[] patternCharacters, LoopingSelector[] selectors)
		{
			if (pattern == null)
			{
				throw new ArgumentNullException("pattern");
			}
			if (patternCharacters == null)
			{
				throw new ArgumentNullException("patternCharacters");
			}
			if (selectors == null)
			{
				throw new ArgumentNullException("selectors");
			}
			if (patternCharacters.Length != selectors.Length)
			{
				throw new ArgumentException("Arrays must contain the same number of elements.");
			}
			List<Class32<int, LoopingSelector>> list = new List<Class32<int, LoopingSelector>>(patternCharacters.Length);
			for (int i = 0; i < patternCharacters.Length; i++)
			{
				list.Add(new Class32<int, LoopingSelector>(pattern.IndexOf(patternCharacters[i]), selectors[i]));
			}
			IEnumerable<Class32<int, LoopingSelector>> arg_8F_0 = list;
			if (ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate4 == null)
			{
				ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate4 = new Func<Class32<int, LoopingSelector>, bool>(ValuePickerBasePage<T>.smethod_0);
			}
			IEnumerable<Class32<int, LoopingSelector>> arg_B1_0 = Enumerable.Where<Class32<int, LoopingSelector>>(arg_8F_0, ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate4);
			if (ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate5 == null)
			{
				ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate5 = new Func<Class32<int, LoopingSelector>, int>(ValuePickerBasePage<T>.smethod_1);
			}
			IEnumerable<Class32<int, LoopingSelector>> arg_D3_0 = Enumerable.OrderBy<Class32<int, LoopingSelector>, int>(arg_B1_0, ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate5);
			if (ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate6 == null)
			{
				ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate6 = new Func<Class32<int, LoopingSelector>, LoopingSelector>(ValuePickerBasePage<T>.smethod_2);
			}
			IEnumerable<LoopingSelector> arg_F5_0 = Enumerable.Select<Class32<int, LoopingSelector>, LoopingSelector>(arg_D3_0, ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate6);
			if (ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate7 == null)
			{
				ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate7 = new Func<LoopingSelector, bool>(ValuePickerBasePage<T>.smethod_3);
			}
			return Enumerable.Where<LoopingSelector>(arg_F5_0, ValuePickerBasePage<T>.CS$<>9__CachedAnonymousMethodDelegate7);
		}
		private void method_6()
		{
			ValueWrapper<T> newWrapper = this.GetNewWrapper(this.Value);
			if (newWrapper != null && this._primarySelectorPart != null && this._secondarySelectorPart != null && this._tertiarySelectorPart != null)
			{
				this._primarySelectorPart.DataSource.SelectedItem = newWrapper;
				this._secondarySelectorPart.DataSource.SelectedItem = newWrapper;
				this._tertiarySelectorPart.DataSource.SelectedItem = newWrapper;
				return;
			}
		}
		protected abstract ValueWrapper<T> GetNewWrapper(T? value);
		protected override void OnNavigatedFrom(NavigationEventArgs navigationEventArgs_0)
		{
			if (navigationEventArgs_0 == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedFrom(navigationEventArgs_0);
			if ("app://external/" == navigationEventArgs_0.get_Uri().ToString())
			{
				base.get_State().set_Item(ValuePickerBasePage<T>.StateKeyValue, this.Value);
			}
		}
		protected override void OnNavigatedTo(NavigationEventArgs navigationEventArgs_0)
		{
			if (navigationEventArgs_0 == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedTo(navigationEventArgs_0);
			bool flag = false;
			if (base.get_State().ContainsKey(ValuePickerBasePage<T>.StateKeyValue))
			{
				this.Value = (base.get_State().get_Item(ValuePickerBasePage<T>.StateKeyValue) as T?);
				if (base.get_NavigationService().get_CanGoBack())
				{
					base.get_NavigationService().GoBack();
					flag = true;
				}
			}
			if (flag)
			{
				return;
			}
			this.InitDataSource();
			this.method_6();
		}
		public abstract void InitDataSource();
		[CompilerGenerated]
		private static bool smethod_0(Class32<int, LoopingSelector> class32_0)
		{
			return -1 != class32_0.Prop_0;
		}
		[CompilerGenerated]
		private static int smethod_1(Class32<int, LoopingSelector> class32_0)
		{
			return class32_0.Prop_0;
		}
		[CompilerGenerated]
		private static LoopingSelector smethod_2(Class32<int, LoopingSelector> class32_0)
		{
			return class32_0.Prop_1;
		}
		[CompilerGenerated]
		private static bool smethod_3(LoopingSelector loopingSelector_0)
		{
			return null != loopingSelector_0;
		}
	}
}
